package com.apes.scm.masterdata.product.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;


/**
 * @description: 资产类别
 * @author: PengZiFeng
 * @create: 2021-08-13 16:41
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FaCategory.CONST_TABLE_NAME)
public class FaCategory extends Model{

    public final static  String CONST_TABLE_NAME = "fa_category";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "资产类别", padding = 8)
    @Comment("类别编码")
    private String id;

    @Column(name = "name")
    @Comment("类别名称")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parents_id", referencedColumnName = "id")
    @Comment("上级类别编码")
    private FaCategory parents;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "asset_type", referencedColumnName = "id")
    @Comment("资产类型")
    private Enumeration assetType;

    @Column(name = "accrual_attribute")
    @Comment("计提属性")
    private String accrualAttribute;

    @Column(name = "measure_unit")
    @Comment("计量单位")
    private String measureUnit;

    @Column(name = "net_residual_value_rate")
    @Comment("净残值率(%)")
    private Double netResidualValueRate;

    @Column(name = "year")
    @Comment("年")
    private Integer year;

    @Column(name = "month")
    @Comment("月")
    private Integer month;

    @ManyToOne
    @JoinColumn(name = "depreciation_method_id",referencedColumnName = "id")
    @Comment("折旧方法")
    private FaDepreciationMethod depreciationMethod;

    @Column(name = "transaction_type")
    @Comment("交易类型")
    private String transactionType;

    @Column(name = "levels", nullable = false)
    @Comment("级别")
    private int levels;

    @Column(name = "is_last_stage")
    @Comment("末级标记")
    private boolean lastStage;

    @Column(name = "deferred_assets")
    @Comment("递延资产")
    private boolean deferredAssets;

    @Column(name = "enabled_state")
    @Comment("启动状态")
    private boolean enabledState;

    @Column(name = "reusable_materials")
    @Comment("周转材料")
    private boolean reusableMaterials;

    @Transient
    private String path;

    public String getPath() {
        String path = null;
        if (parents != null) path = parents.getPath();
        return path != null ? path + "/" + id : id;
    }

}
